<template>
  <div>
  <!-- 按钮和搜索栏容器 -->
  <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px;">
    <div style="display: flex; gap: 5px;">
      <el-button v-show="returnButton" @click="returnLists()"><el-icon><Back /></el-icon></el-button>
    </div>
    <!-- 搜索栏 -->
    <div>
      <el-input
          v-model="searchname"
          placeholder="请输入会议室名称、员工名称、维修原因"
          style="width: 250px;"
          @keyup.enter="search()"
      >
        <template #append>
          <el-button @click="search()"><el-icon><Search /></el-icon></el-button>
        </template>
      </el-input>
    </div>
  </div>

  <el-table id="mytable" :data="filteredData" stripe border style="width: 100%">
    <el-table-column fixed prop="id" label="会议室事件id" width="80" align="center"/>
    <el-table-column prop="roomName" label="会议室名称" width="130" align="center"/>
    <el-table-column label="事件创建时间" align="center">
       <template #default="scope">
          {{ formatCreateTime(scope.row.createTime) }}
       </template>
    </el-table-column>
    <el-table-column label="事件开始时间" align="center">
       <template #default="scope">
          {{ formatCreateTime(scope.row.startTime) }}
       </template>
    </el-table-column>
    <el-table-column label="事件结束时间" align="center">
       <template #default="scope">
          {{ formatCreateTime(scope.row.endTime) }}
       </template>
    </el-table-column>
    <el-table-column label="事件更新时间" align="center">
       <template #default="scope">
          {{ formatCreateTime(scope.row.updateTime) }}
       </template>
    </el-table-column>
     <el-table-column label="事件状态" align="center">
      <template #default="scope">
        {{ formatStatus(scope.row.status) }}
      </template>
    </el-table-column>
    <el-table-column label="操作" width="180" align="center">
      <template v-slot="scope">
        <el-button v-if="scope.row.status==='reserved'" link @click="handleUsing(scope.row.id)" type="primary" size="big">开始使用会议室</el-button>
        <el-button v-if="scope.row.status==='in_use'" link @click="handleFinished(scope.row.id,'')" type="primary" size="big">空闲</el-button>
        <el-button v-if="scope.row.status==='in_use'" link @click="handleFinishedDialog(scope.row.id)" type="primary" size="big">维护</el-button>
        <el-button v-if="scope.row.status==='maintenance'" link @click="handleLookingReason(scope.row)" type="primary" size="big">查看维护原因</el-button>
        <el-button v-if="scope.row.status==='maintenance'" link @click="handleMaintained(scope.row.id)" type="primary" size="big">维护结束</el-button>
      </template>
    </el-table-column>
  </el-table>
  <!-- 分页 -->
  <!-- 分页组件 -->
  <el-pagination
    @current-change="handleCurrentChange"
    :current-page="currentPage"
    :page-size="pageSize"
    :total="totalCount"
  >
  </el-pagination>
   <el-dialog v-model="finishDialogVisible" title="填写维护理由">
      <el-form >
        <el-form-item label="理由">
          <el-input type="textarea" v-model="reason" />
        </el-form-item>
         <el-form-item label="开始时间">
        <el-date-picker
            v-model="startTime"
            type="datetime"
            placeholder="请选择开始时间"
            value-format="YYYY-MM-DD hh:mm:ss"
        />
      </el-form-item>
      <el-form-item label="结束时间">
        <el-date-picker
           v-model="endTime"
           type="datetime"
           placeholder="请选择开始时间"
           value-format="YYYY-MM-DD HH:mm:ss"
         />
      </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="finishDialogVisible= false">取消</el-button>
        <el-button type="primary" @click="handleFinished(this.id,'maintenance')">提交</el-button>
      </template>
   </el-dialog> 
   <el-dialog v-model="reasonDialogVisible" title="查看维护理由">
      <p>{{ this.reason }}</p>
      <template #footer>
        <el-button @click="reasonDialogVisible= false;reason=''">取消</el-button>
      </template>
   </el-dialog> 
  </div>
</template>

<script>
import axios from "axios";
import { Search } from '@element-plus/icons-vue';
import { Back} from "@element-plus/icons-vue";
import { ElMessageBox } from 'element-plus';
export default {
  name: "LostAndFoundList",
  data() {
    return {
      listData: [],
      filteredData: [],
      searchname: '',
      returnButton:false,
      currentPage:1,
      pageSize:10,
      totalCount:0,  
      cancelDetailVisible: false,  
      cancelForm:{},
      finishDialogVisible:false,
      reasonDialogVisible:false,
      reason:'',
      id:'',
      startTime:'',
      endTime:'',
    }
  },
  components :{
     Search,
     Back,
  },
  mounted() {
    this.fetchData();
  },
  methods: {
    handleCurrentChange(page){
        this.currentPage=page;
        if(!this.searchname){
        this.fetchData();
        }else{
          this.search();
        }
    },
    fetchData(){
        axios.get("/staff/room-status/pending",{
          params:{
            pageNum:this.currentPage,
            pageSize:this.pageSize,
          }
        })
        .then(res=>{
        
          this.listData=res.data.data.records;
          this.totalCount=res.data.data.total;
          if (Array.isArray(this.listData)) {
            this.filteredData = this.listData;
          } else {
              console.error('加载错误');
          }
        }).catch(err=>{
          this.$message.error(err.data.code||"获取列表失败");
        })
    },
    search() {
      this.returnButton=true;
      axios.get('/staff/room-status/pending',{
          params:{
            keyword:this.searchname,
            pageNum:this.currentPage,
            pageSize:this.pageSize,
          }
        })
      .then(res =>{
        if(res.data.code===200){
          this.filteredData=res.data.data.records;
          this.totalCount=res.data.data.total;
          this.$message.success("查询成功");
        }else {
          this.$message.error(res.data.msg||"查询失败");
        }
      })
    },
    returnLists(){
      this.returnButton=false;
      this.searchname=null;
      this.currentPage=1;
      this.fetchData();
    },
    goBackToList() {
      this.fetchData();
    },
    formatCreateTime(time){
      if(time){
        return time.replace('T',' ');
      }
      return '无'
    },
    formatStatus(status){
      if(status==='reserved'){
        return '预订未使用';
      }else if(status==='in_use'){
        return '使用中';
      }else {
        return '维护中';
      }
    },
    handleUsing(id){
       ElMessageBox({
        message: '确定要使用此会议室吗？',
        type: 'warning',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        showCancelButton: true,
      }).then(()=>{
       axios.post('/staff/room-status/using/'+id).then(res=>{
        if(res.data.code===200){
          this.$message.success('设置成功！');
          this.fetchData();
        }else {
          this.$message.error(res.data.msg || '设置失败！');
        }
       }) 
      }).catch(()=>{})
    },
    handleMaintained(id){
       ElMessageBox({
        message:  '确定要结束此次维护吗？',
        type: 'warning',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        showCancelButton: true,
      }).then(()=>{
       axios.post('/staff/room-status/maintained/'+id).then(res=>{
        if(res.data.code===200){
          this.$message.success('设置成功！');
          this.fetchData();
        }else {
          this.$message.error(res.data.msg || '设置失败！');
        }
       }) 
      }).catch(()=>{})
    },
    handleFinishedDialog(id){
      this.finishDialogVisible=true;
      this.id=id;
    },
    handleFinished(id,status){
      ElMessageBox({
        message: status==='' ? '确定要将此会议室设置为空闲吗？' : '确定要维护此会议室吗？',
        type: 'warning',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        showCancelButton: true,
      }).then(()=>{
      axios.post('/staff/room-status/used',{
          startTime:this.startTime,
          endTime:this.endTime,
          statusLogId:id,
          status:status,
          reason:this.reason,
        
      }).then(res=>{
        if(res.data.code===200){
           this.$message.success('设置成功！');
          this.fetchData();
        }else {
          this.$message.error(res.data.msg || '设置失败！');
        } 
      })}).catch(()=>{})

    },
    handleLookingReason(row){
      this.reason=row.reason;
       this.reasonDialogVisible=true;
    }
  },
}

</script>

<style scoped>
.clicked-s {
  background-color: green;
  color: white;
}
</style>